package com.interf.agv;

import com.yy.base.page.Message;
import com.yy.base.util.SystemProperties;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class AGVUtil {
	//获取AGV列表
	private static final long CMD_GET_AGV_LIST = 0x00010001;
	//关闭AGV系统
	private static final long CMD_SHUTDOWN_SYSTEM = 0x00010002;
	//暂停AGV系统
	private static final long CMD_STOP_SYSTEM = 0x00010003;
	//恢复AGV系统
	private static final long CMD_RESUME_SYSTEM = 0x00010004;
	//增加AGV小车
	private static final long CMD_ADD_AGV = 0x00010005;
	//删除AGV小车
	private static final long CMD_DELETE_AGV = 0x00010006;
	//检修AGV小车
	private static final long CMD_REPAIR = 0x00010007;
	//去充电
	private static final long CMD_POWER = 0x00010009;
	//任务调度
	private static final long CMD_TASKSCADURE = 0x0001000a;
	//随机跑
	private static final long CMD_START_RANDOM_RUN = 0x0001000f;
	//关闭随机跑
	private static final long CMD_END_RANDOM_RUN = 0x00010010;
	//定义socket
	private static Socket socket;
	//定义inputStream
	private static InputStream inputStream;
	//定义outPutStream
	private static OutputStream outputStream;

	private static void init() {
		try {
			socket = initSocket();
			Thread.sleep(1000);
			inputStream = socket.getInputStream();
			outputStream = socket.getOutputStream();

			//获取消息
			new AGVData(inputStream).start();
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	/**
	 * 验证是否连接在线
	 */
	public static void checkConnection() {
		if (socket != null && socket.isConnected() && !socket.isClosed()) {
			return;
		} else {
			if (socket != null) {
				try {
					socket.close();
				} catch (Exception e) {
				}
			}
			socket = null;
			init();
		}
	}

	/**
	 * 任务调度功能，需要传入json格式的字符串
	 * @param jsonStr
	 * @param cmdOrder
	 * @return
	 */
	public static Message sendAGVTask(String jsonStr, long cmdOrder) {
		Message msg = new Message();
		try {
			checkConnection();

			//要传给服务端的是byte数组
			byte[] orderByte;
			//此命令表示无body传入，仅出入cmd指令
			if (jsonStr.equalsIgnoreCase("NONE")) {
				orderByte = AGVHandler.getTaskInfo(cmdOrder);
			} else {
				orderByte = AGVHandler.getTaskInfo(cmdOrder, jsonStr);
			}
			try {
				//获取socket的出入流
				getOutputStream().write(orderByte);
				getOutputStream().flush();

				//msg.setData("data", getResponse());
			} catch (Exception e) {
				msg.setErrorMessage("调用AGV异常：" + e.getMessage());
				return msg;
			}
			msg.setOkMessage("调用AGV指令发送成功。");
		} catch (Exception e) {
			msg.setErrorMessage("调用AGV指令发送失败：" + e.getMessage());
		} finally {
			close();
		}
		return msg;
	}

	/**
	 * 初始化Socket
	 * @return
	 */
	private static Socket initSocket() {
		if (socket == null) {
			try {
				String ip = SystemProperties.getValue("agv-socket-ip");
				String host = SystemProperties.getValue("agv-socket-host");
				socket = new Socket(ip, Integer.parseInt(host));
			} catch (IOException e) {
				e.printStackTrace();
				throw new RuntimeException("socket初始化失败。");
			}
			return socket;
		} else {
			return socket;
		}
	}

	/**
	 * 获取输出流
	 * @throws IOException
	 */
	private static OutputStream getOutputStream() throws IOException {
		if (outputStream == null) {
			outputStream = socket.getOutputStream();
		}
		return outputStream;
	}

	public static void close() {
		try {
			Thread.sleep(300);
		} catch (Exception e) {
		}
		try {
			inputStream.close();
		} catch (IOException ex) {
			ex.printStackTrace();
		}
		try {
			outputStream.close();
		} catch (IOException ex) {
			ex.printStackTrace();
		}
		try {
			socket.close();
		} catch (IOException ex) {
			ex.printStackTrace();
		}
		socket = null;
	}

	public static void main(String[] args) {
		String code = "MO1910170055";

		String jsonStr= "{\"task_seq\":\"" + code + "\",\"submd\":1,\"flyloom_id\":16," +
				"\"dest_position\":{\"x\":0,\"y\":0}" +
				"}";
		sendAGVTask(jsonStr, 0x00060001);
	}
}
